/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package lib.plan;

import java.util.ArrayList;
import java.util.List;
import lib.xml.XMLizable;
import lib.model.distribution.Distribution;

/**
 * Item class represents the item which are produced by the factory. They have a
 * collection of parameters for being built. They also keep information of how
 * many task have been done to them
 * @author Jorge Mira Yagüe
 */
public class Item implements XMLizable {

    private int number;
    private int order;
    private int priority;
    private String category;
    private List<Distribution> parameters;
    private int task;

    /**
     * Builds an empty item
     */
    public Item() {
        number = 0;
        order = 0;
        priority = 0;
        category = "";
        parameters = new ArrayList<Distribution>();
        task = 0;
    }

    /**
     * Builds an Item with the given parameters
     * @param category the category of the item
     * @param priority the priority of the item
     * @param parameters the parameters of the tasks
     */
    public Item(String category,
            int priority,
            List<Distribution> parameters) {
        this.category = category;
        this.parameters = parameters;
        this.priority = priority;
        task = 0;
    }

    public List<Distribution> getParameters() {
        return parameters;
    }

    public void setParameters(List<Distribution> parameters) {
        this.parameters = parameters;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int itemNumber) {
        this.number = itemNumber;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public void setParameter(int index, Distribution distribution) {
        parameters.set(index, distribution);
    }

    public Distribution getParameter(int index) {
        return parameters.get(index);
    }

    public Distribution getActualParameter() {
        return parameters.get(task);
    }

    public int getNumTasks() {
        return parameters.size();
    }

    public int getTaskNum() {
        return task;
    }

    public void increaseTaskNum() {
        this.task++;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public boolean isFinished() {
        return parameters.size() <= task;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    /**
     * Restarts the task index of the item
     */
    public void flush() {
        task = 0;
    }

    public String toXML() {
        String result = "<Item ";
        result += "priority=\"" + priority + "\" ";
        result += "category=\"" + category + "\" ";
        result += ">\n";
        if (parameters != null) {
            for (Distribution dist : parameters) {
                result += dist.toXML();
            }
        }
        result += "</Item>\n";
        return result;
    }

    @Override
    public String toString() {
        return category + " " + priority;
    }
}
